﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Twillo.Core.Settings;

namespace TwilloClient.Forms
{
    public partial class frmMain : Form
    {

        public Int32 RefreshIntervalSeconds
        {
            set
            {
                this.cRefreshTimer.Interval = value * 1000;
                this.cRefreshTimer.Enabled = true;
            }
        }

        public frmMain()
        {
            InitializeComponent();
        }

        private void cNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.WindowState = FormWindowState.Minimized;
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            RefreshTweets();
            tabControl1.SelectedIndex = 0;
        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {
            this.cTimeLine.Repaint(this.splitContainer1.Panel1.Width - 30);
        }

        private void cTimeLine_LinkClicked(object sender, Twillo.Core.Controls.TwitterStatusLabelClickEventArgs e)
        {
            this.cUserProfile.TwitterUser = Twillo.Core.Twitter.GetUser(e.Data);
        }

        private void cRefreshTimer_Tick(object sender, EventArgs e)
        {
            RefreshTweets();
        }

        private void btnTweet_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTweet.Text.Trim()))
            {
                MessageBox.Show("No Status", "Nothing to Tweet!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            Twillo.Core.Twitter.PostUpdate(txtTweet.Text.Trim());
            txtTweet.Text = "";
            btnTweet.Enabled = false;
        }

        private void txtTweet_TextChanged(object sender, EventArgs e)
        {
            btnTweet.Enabled = !string.IsNullOrEmpty(txtTweet.Text.Trim());
        }

        public void RefreshTweets()
        {
            this.Cursor = Cursors.WaitCursor;
            this.cTimeLine.Items = Twillo.Core.Twitter.GetFriendTimeLine(true, SettingsManager.TimeLineCount);
            cUserProfile.TwitterUser = Twillo.Core.Twitter.Self();
            pnlTrends.Trends = Twillo.Core.Twitter.GetCurrentTrends();
            cPubTweets.Items = Twillo.Core.Twitter.GetPublicTimeLine(true, SettingsManager.TimeLineCount);
            this.Cursor = Cursors.Default;
            SetStatus("Last Refresh on " + DateTime.Now.ToLongDateString() + " at " + DateTime.Now.ToLongTimeString());
        }

        private void SetStatus(string text)
        {
            this.toolStripStatusLabel1.Text = text;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshTweets();
        }
    }
}
